﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Controller
{
    public class KPIMethodController : Controller.dbConnection.ConnectionClass
    {
        public bool Save(List<KPIMethod> list,int KpiID)
        {
            bool result = false;


            string connectionString = ConfigurationManager.ConnectionStrings["connstring"].ToString();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {
                   
                    string query = "SP_KPIMethod_Delete_ByKpiId";
                    SqlCommand command = new SqlCommand(query, conn);
                    command.CommandType = CommandType.StoredProcedure;
                    command.Parameters.Add("@KpiId", SqlDbType.Int).Value = KpiID;


                    command.Transaction = txn;
                    command.ExecuteNonQuery();
                    command.Dispose();

                    
                        foreach (KPIMethod data in list)
                        {
                            query = "SP_KPIMethod_Insert";
                            command = new SqlCommand(query, conn);
                            command.CommandType = CommandType.StoredProcedure;
                            command.Parameters.Add("@KpiID", SqlDbType.Int).Value = data.KpiId;
                            command.Parameters.Add("@MethodDtlId", SqlDbType.Int).Value = data.MethodDtlId;
                            command.Parameters.Add("@Status", SqlDbType.Bit).Value = data.Status;
                            command.Parameters.Add("@MethodID", SqlDbType.Int).Value = data.MethodID;
                            command.Parameters.Add("@isOneSelection", SqlDbType.Bit).Value = data.isOneSelection;
                            command.Parameters.Add("@CreateBy", SqlDbType.VarChar).Value = data.CreateBy;
                            command.Parameters.Add("@CreateDate", SqlDbType.Date).Value = data.CreateDate;
                            command.Parameters.Add("@CountMethodBy", SqlDbType.VarChar).Value = data.CountMethodBy;
                            command.Parameters.Add("@MaxDay", SqlDbType.VarChar).Value = data.MaxDay;
                            command.Parameters.Add("@UoMID", SqlDbType.VarChar).Value = data.UoMID;
                            command.Parameters.Add("@UoMDetailIDMax", SqlDbType.VarChar).Value = data.UoMDetailIDMax;
                            command.Parameters.Add("@UoMDetailIDMin", SqlDbType.VarChar).Value = data.UoMDetailIDMin;
                            command.Parameters.Add("@PercentageMax", SqlDbType.VarChar).Value = data.PercentageMax;
                            command.Parameters.Add("@PercentageMin", SqlDbType.VarChar).Value = data.PercentageMin;

                            command.Transaction = txn;
                            command.ExecuteNonQuery();
                            command.Dispose();
                        }
                   



                    result = true;
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }



                return result;
            }
        }

        public KPIMethod GetByKpiId(int KpiID)
        {
            KPIMethod obj = null; 
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_KPIMethod_By_KpiId";
            command.Parameters.Add("@KpiId", SqlDbType.Int).Value = KpiID;

            DataSet dx = GetTable(command);

            foreach (DataRow dr in dx.Tables[0].Rows)
            {
                obj = new KPIMethod();
                obj.MethodID = Convert.ToInt32(dr["MethodID"].ToString().Trim());
                obj.isOneSelection = Convert.ToBoolean(dr["isOneSelection"].ToString().Trim());
                obj.KpiId = Convert.ToInt32(dr["KpiId"].ToString().Trim());
            }

            return obj;
        }

        public List<KPIMethod> GetListByKpiId(int KpiID)
        {
            List<KPIMethod> list = new List<KPIMethod>();

            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "SP_KPIMethod_Select_ByKpiId";
            command.Parameters.Add("@KpiId", SqlDbType.Int).Value = KpiID;

            DataSet dx = GetTable(command);

            foreach (DataRow dr in dx.Tables[0].Rows)
            {
                KPIMethod obj = new KPIMethod();
                obj.KpiId = Convert.ToInt32(dr["KpiId"].ToString().Trim());
                obj.MethodDtlId = Convert.ToInt32(dr["MethodDtlId"].ToString().Trim());
                obj.Status = Convert.ToBoolean(dr["Status"].ToString().Trim());
                obj.MethodID = Convert.ToInt32(dr["MethodID"].ToString().Trim());
                obj.MethodDetail.MethodDtlName = dr["MethodDtlName"].ToString().Trim();
                obj.MethodDetail.MethodDetailID = Convert.ToInt32(dr["MethodDtlId"].ToString().Trim());
                obj.MethodDetail.MethodID = Convert.ToInt32(dr["MethodID"].ToString().Trim());
                obj.isOneSelection = Convert.ToBoolean(dr["isOneSelection"].ToString().Trim());
                obj.MaxDay = Convert.ToInt32(dr["MaxDay"].ToString().Trim());
                obj.UoMID = Convert.ToInt32(dr["UoMID"].ToString().Trim());
                obj.UoMDetailIDMax = Convert.ToInt32(dr["UoMDetailIDMax"].ToString().Trim());
                obj.UoMDetailIDMin = Convert.ToInt32(dr["UoMDetailIDMin"].ToString().Trim());
                obj.PercentageMax = Convert.ToInt32(dr["PercentageMax"].ToString().Trim());
                obj.PercentageMin = Convert.ToInt32(dr["PercentageMin"].ToString().Trim());
                list.Add(obj);
            }

            return list;
        }
    }
}